﻿
SET QUOTED_IDENTIFIER ON
GO

SET ANSI_NULLS ON
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_WA_RecordOffDutyData]') AND type in (N'P', N'PC'))
    DROP PROCEDURE [dbo].[sproc_WA_RecordOffDutyData];
GO
CREATE PROCEDURE [dbo].[sproc_WA_RecordOffDutyData]--记录下班状态
	@Staff_ID   int,
    @dutytime	datetime, --下班时间
    @dutystatus bit,      --是否早退
    @memo		nvarchar(400) --描述理由
AS
BEGIN
SET NOCOUNT ON
SET XACT_ABORT ON

declare @dayValue int;
set @dayValue = dbo.fn_GetDayValue(@dutytime);

--检查是否早退，如果是就向UDS_WorkAttendence_Memo插记录
IF (@dutystatus=0 ) --没有早退
begin
    update dbo.UDS_WorkAttendence_Data
    set
		OffDuty = @dutytime,
		OffDuty_Status = @dutystatus
	where
		StaffID = @Staff_ID 
		and DayValue = @dayValue;
    return @@error
end
else
begin
    begin transaction;
    
    IF((@memo is not null) and (@memo<>''))
    BEGIN
        declare @memoid bigint;
        insert into dbo.UDS_WorkAttendence_Memo(Memo) values(@memo);

        set @memoid = @@identity;
        update dbo.UDS_WorkAttendence_Data set OffDuty = @dutytime, OffDuty_Status = @dutystatus, offDuty_MemoID = @memoid where StaffID = @Staff_ID and DayValue = @dayValue;
    END
    ELSE
    BEGIN
        update dbo.UDS_WorkAttendence_Data set OffDuty=@dutytime,OffDuty_Status=@dutystatus where StaffID = @Staff_ID and DayValue = @dayValue;
    END
    
    commit transaction;
    
    return @@error
end

END
GO